package com.bio.dao.user.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.criterion.Restrictions;

import com.bio.dao.impl.HibernateDaoImpl;
import com.bio.dao.impl.HibernateQuery;
import com.bio.dao.user.PositionDao;
import com.bio.entity.user.Position;
import com.bio.utils.Pager;
import com.bio.utils.Pagination;

public class PositionDaoImpl extends HibernateDaoImpl<Position> implements PositionDao{

	private static final Integer FLAG_DELETED = 1;
	
	@Override
	public Pager<Position> findPagerByCompanyId(Pagination pagination, Long companyId) {
		return new HibernateQuery<Position>(this.getSessionFactory(), 
				this.getSession(), Position.class).setPagination(pagination)
				                                  .addWhere(Restrictions.eq("companyId",companyId)).query();
	}

	@Override
	public List<Position> findAllByCompanyId(Long companyId) {
		return new HibernateQuery<Position>(this.getSessionFactory(), 
				this.getSession(), Position.class).setPagination(null)
				                                  .addWhere(Restrictions.eq("companyId",companyId)).query().getResult();
	}

	@Override
	public void removePositionsBySuperior(Long superiorId) {
        String sql = "UPDATE position SET is_deleted = " + FLAG_DELETED + " WHERE superior_id =" + superiorId;
        SQLQuery sq = this.getSession().createSQLQuery(sql);
        sq.executeUpdate();
	}

	@Override
	public void removePositionsByCompany(Long companyId) {
		String sql = "UPDATE position SET is_deleted = " + FLAG_DELETED + " WHERE company_id =" + companyId;
        SQLQuery sq = this.getSession().createSQLQuery(sql);
        sq.executeUpdate();
	}

	@Override
	public List<Position> findAllBySuperior(Long superiorId) {
		return new HibernateQuery<Position>(this.getSessionFactory(), 
				this.getSession(), Position.class).setPagination(null)
				                                  .addWhere(Restrictions.eq("superiorId",superiorId)).query().getResult();
	}

}
